package com.hbisedm.interninfosys.intern.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbisedm.interninfosys.commonutils.R;
import com.hbisedm.interninfosys.intern.entity.User;
import com.hbisedm.interninfosys.intern.entity.UserInternship;
import com.hbisedm.interninfosys.intern.entity.UserLeave;
import com.hbisedm.interninfosys.intern.entity.queryVo.UserQuery;
import com.hbisedm.interninfosys.intern.service.IUserLeaveService;
import com.hbisedm.interninfosys.intern.service.IUserService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Sam
 * @since 2020-12-03
 */
@RestController
@RequestMapping("/intern/user-leave")
public class UserLeaveController {
    @Autowired
    IUserLeaveService userLeaveService;
    @Autowired
    IUserService userService;

    @PostMapping("/list/{current}/{limit}")
    public R leaveList(@PathVariable long current,
                          @PathVariable long limit,
                          @RequestBody(required = false) UserQuery leaveQuery) {

        Page<UserLeave> userLeavePage = new Page<>(current, limit);
        String associationName = leaveQuery.getAssociationName();
        QueryWrapper<UserLeave> userLeaveQueryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(associationName)) {
            User one = userService.getOne(new QueryWrapper<User>().lambda().eq(User::getUsername, associationName));
            Integer iid = one.getId();
            userLeaveQueryWrapper.lambda().eq(UserLeave::getIid, iid);
        }
        userLeaveService.page(userLeavePage,userLeaveQueryWrapper);
        List<UserLeave> list = userLeavePage.getRecords();
        for (UserLeave leave : list) {
            User stu = userService.getById(leave.getSid());
            leave.setStuName(stu.getOthername());
            User intern = userService.getById(leave.getIid());
            leave.setInternName(intern.getOthername());
        }
        return R.ok()
                .data("userLeaves", list)
                .data("total", list.size());
    }

    @GetMapping("/info/{id}")
    public R leaveInfo(@PathVariable Integer id) {
        UserLeave leave = userLeaveService.getById(id);
        User stu = userService.getById(leave.getSid());
        leave.setStuName(stu.getOthername());
        User intern = userService.getById(leave.getIid());
        leave.setInternName(intern.getOthername());
        return R.ok().data("leave", leave);
    }

    @PostMapping("/saveOrUpdate")
    public R leaveUpdate(@RequestBody UserLeave userLeave) {
        System.out.println(userLeave);
        boolean b = userLeaveService.saveOrUpdate(userLeave);
        if(b) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @DeleteMapping("/delete/{id}")
    public R leaveDelete(@PathVariable Integer id) {
        boolean b = userLeaveService.removeById(id);
        if(b) {
            return R.ok();
        }else {
            return R.error();
        }
    }


    /**
     * 获取当前学生的评价记录
     * @param userLeave
     * @return
     */
    @PostMapping("/getListBySid")
    public R getListBySid(@RequestBody UserLeave userLeave) {
        List<UserLeave> list = userLeaveService.list(new QueryWrapper<UserLeave>().lambda().eq(UserLeave::getIid, userLeave.getIid()).eq(UserLeave::getSid, userLeave.getSid()));
        if (list.size() == 0) {
            return R.ok();
        } else {
            return R.ok().data("list", list);
        }
    }

    /**
     * 获取iid的对应请假列表
     * @param userLeave
     * @return
     */
    @PostMapping("/getListByiid")
    public R getListByiid(@RequestBody UserLeave userLeave) {
        List<UserLeave> list = userLeaveService.list(new QueryWrapper<UserLeave>().lambda().eq(UserLeave::getIid, userLeave.getIid()));
        return R.ok().data("list", list);
    }

    /**
     * 前台企业审核学生请假
     * @param userLeave
     * @return
     */
    @PostMapping("/checkLeave")
    public R checkLeave(@RequestBody UserLeave userLeave) {
        UserLeave target = userLeaveService.getById(userLeave.getId());
        target.setLeaveStatus(true);
        boolean b = userLeaveService.saveOrUpdate(target);
        return R.ok().data("status", b);
    }

}
